package com.ruicar.afs.cloud.afscase.common.utils;

import com.ruicar.afs.cloud.common.core.uid.AfsSequenceGenerator;
import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @param
 * @author yk.Li
 * @description 序列号生成工具类
 * @date 2020/7/15
 */
@UtilityClass
public class SequenceUtil {
    final AfsSequenceGenerator afsSequenceGenerator;

    static {
        afsSequenceGenerator = SpringContextHolder.getBean(AfsSequenceGenerator.class);
    }

    /**
     * 固定格式生成序列号
     * 20200715-1
     * 20200715-2
     * 20200715-3
     * 隔日重置序号为1
     *
     * @param type
     * @return
     */
    public static String getSeq(String type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        StringBuffer prefix = new StringBuffer(sdf.format(new Date()));
        Long seqno = afsSequenceGenerator.genNext(type, prefix.toString());
        return prefix
                .append("-")
                .append(StringUtils.leftPad(String.valueOf(seqno), 2, '0'))
                .toString();
    }
}
